package com.terrier.ui.managers
{
    import com.terrier.ui.core.WxApplication;
    import com.terrier.ui.core.WxSprite;
    import com.terrier.ui.events.WxSystemEvent;
    
    import flash.display.DisplayObjectContainer;
    import flash.display.Stage;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.EventDispatcher;

    /**
     * 系统管理器
     *
     * @author Jarvis.weng
     */
    public class WxSystemManager extends EventDispatcher
    {
        private static var s_instance:WxSystemManager;

        private static var _root:DisplayObjectContainer; // 文档根类1

        private static var _stage:Stage // 主场景

        private static var _mainLayer:WxSprite; // 主层

        private static var _popupLayer:WxSprite; // 弹出窗口层

        private static var _toolTipLayer:WxSprite; // 工具提示层

        private static var _animationLayer:WxSprite;

        private static var _cursorLayer:WxSprite; // 鼠标层

        public static function get instance():WxSystemManager
        {
            if (s_instance == null)
            {
                s_instance = new WxSystemManager(new Singleton());
            }
            return s_instance;
        }

        /**
         * 构造函数
         */
        public function WxSystemManager(singleton:Singleton)
        {
			if (singleton == null)
			{
				throw new Error("不允许直接实例化该类型");
			}
			
            createLayers();
        }

        /**
         * 设置swf文档根对象.如果swf主文档类没有继承WxApplication类,那么在使用组件之前,需要手动调用该方法
         * @param root
         */
        public function setRoot(root:DisplayObjectContainer):void
        {
            if (root == null)
            {
                throw new Error("the root object can not be null");
            }

            _root = root;

            if (_root is Stage || _root.stage)
            {
                initStage();
            }
            else
            {
                _root.addEventListener(Event.ADDED_TO_STAGE, initStage);
            }
        }

        /**
         * swf文档根对象.
         * @return
         */
        public function get root():DisplayObjectContainer
        {
            return _root;
        }

        /**
         * stage对象
         * @return
         */
        public function get stage():Stage
        {
            return _stage;
        }

        /**
         *
         * @return
         */
        public function get popupLayer():DisplayObjectContainer
        {
            return _popupLayer;
        }

        /**
         *
         * @return
         */
        public function get toolTipLayer():DisplayObjectContainer
        {
            return _toolTipLayer;
        }

        /**
         *
         * @return
         */
        public function get cursorLayer():DisplayObjectContainer
        {
            return _cursorLayer;
        } 

        /**
         *
         * @return
         */
        public function get animationLayer():DisplayObjectContainer
        {
            return _animationLayer;
        }

        private function initStage(e:Event = null):void
        {
            _root.removeEventListener(Event.ADDED_TO_STAGE, initStage);

            // 初始化舞台设置
            _stage = _root is Stage ? _root as Stage : _root.stage;
            _stage.scaleMode = StageScaleMode.NO_SCALE;
            _stage.align = StageAlign.TOP_LEFT;

            // 显示管理器的容器层
            showLayer();

            // 初始化内部事件监听器
            initEventListener();

            // 派发舞台初始化事件
            var event:WxSystemEvent = new WxSystemEvent(WxSystemEvent.STAGE_INIT, _stage);
            dispatchEvent(event);
        }

        private function initEventListener():void
        {

        }

        private function createLayers():void
        {
            _popupLayer = new WxSprite();
            _animationLayer = new WxSprite();
            _toolTipLayer = new WxSprite();
            _cursorLayer = new WxSprite();

            _cursorLayer.mouseEnabled = false;
            _cursorLayer.mouseChildren = false;

            _toolTipLayer.mouseEnabled = false;
            _toolTipLayer.mouseChildren = false;

            _animationLayer.mouseEnabled = false;
            _animationLayer.mouseChildren = false;
        }

        private function showLayer():void
        {
            _mainLayer = new WxSprite();
            _mainLayer.mouseEnabled = false;
            _mainLayer.addChild(_popupLayer);
            _mainLayer.addChild(_animationLayer);
            _mainLayer.addChild(_toolTipLayer);
            _mainLayer.addChild(_cursorLayer);

            var app:WxApplication = _root as WxApplication;
            if (app)
            {
                app.addChild(_mainLayer);
            }
            else
            {
                _stage.addChild(_mainLayer);
            }
        }
    }
}

class Singleton
{
	
}